<?php

class Products extends Controller {

	function Products()
	{
		parent::Controller();	

        $this->load->model('pageheadersmodel');   
        $this->load->model('productsmodel'); 

	}     
    
    function index()
    {
                                   
        //load page header
        $this->load->model('pageheadersmodel'); 
        $data["header"] = $this->pageheadersmodel->get_header('products');

        //products list                                
        $all_products = $this->productsmodel->get_all_products();          
        $data["list"]["items"] = $this->productsmodel->group_into_categories($all_products);
        $data["list"]["controller"] = "products/view/";

        $this->load->view('website/default/head', array("document_title" => DOCUMENT_TITLE . "Products"));   
        $this->load->view('website/default/header');  
        $this->load->view('website/products/products', $data);   
        $this->load->view('website/default/footer');   

    }                                                     
    
    function categories($id = FALSE)
    {                                                                             
        if ($id !== FALSE) {

            if ($id == ONE_OFF_CAT_ID) redirect('products/one_off_floors');

            //category name and description
            $data["cat"] = $this->productsmodel->get_category($id);
            if (empty($data["cat"])) show_404();   

            $subcats = $this->productsmodel->get_categories_by_parent($id);  
            //print $this->db->last_query();    
            $products = $this->productsmodel->get_products_by_category($id);                

            if (!empty($subcats)){ // cat has subcategories

                $data["list"]["items"] = $subcats;
                $data["list"]["controller"] = "products/categories/";

            } else if (!empty($products)){ // cat has products

                if ($data["cat"]["Parent_Categories"]!==NULL){ // cat is a subcategory

                    $data["parent_cat"] = $this->productsmodel->get_category($data["cat"]["Parent_Categories"]);
                    $data["list"]["items"] = $products;
                    $data["list"]["controller"] = "products/view/";

                    if (empty($data["parent_cat"])) show_404();                                                 

                } else { // cat is a top cat with products

                    $data["list"]["items"] = $products;
                    $data["list"]["controller"] = "products/view/";  					                 

                } 

            } else show_404(); //cat has no products or subcats


            $this->load->view('website/default/head', array("document_title" => DOCUMENT_TITLE . "Products by category - " . $data["cat"]["Title_Categories"]));  
            $this->load->view('website/default/header');  
            $this->load->view('website/products/prod_category', $data);   
            $this->load->view('website/default/footer');

        } else { // no id is received, show all cats
       
            //load page header
            $this->load->model('pageheadersmodel'); 
            $data["header"] = $this->pageheadersmodel->get_header('products_by_category');

            $data["list"]["items"] = $this->productsmodel->get_top_level_categories();//$this->productsmodel->group_into_categories($all_products);
            $data["list"]["controller"] = "products/categories/";

            $this->load->view('website/default/head', array("document_title" => DOCUMENT_TITLE . "Products by category"));  
            $this->load->view('website/default/header');  
            $this->load->view('website/products/products_by_category', $data);   
            $this->load->view('website/default/footer');
        }
    }    
    function one_off_floors()
    {
          
        $data["cat"] = $this->productsmodel->get_category(ONE_OFF_CAT_ID);
        $data["list"]["items"] =    $this->productsmodel->get_products_by_category(ONE_OFF_CAT_ID);
        $data["list"]["controller"] = "products/view/";
         
        $this->load->view('website/default/head', array("document_title" => DOCUMENT_TITLE . "One-Off Products")); 
        $this->load->view('website/default/header');  
        $this->load->view('website/products/prod_category', $data);   
        $this->load->view('website/default/footer');   

    }
    
    function applications($id = FALSE)
    {

        if ($id === FALSE) {
                                       
            //load page header
            $this->load->model('pageheadersmodel'); 
            $data["header"] = $this->pageheadersmodel->get_header('products_by_application');

            $data["list"] = array();

            $all_apps = $this->productsmodel->get_all_parent_applications();
            foreach($all_apps as $app){

                $products = $this->productsmodel->get_products_by_application($app["id"]);
                $group = array();
                $group["name"] = $app["title"];
                $group["image"] = $app["image"];
                $group["params"] = $app["id"];
                $group["items"] = $this->productsmodel->group_into_categories($products);
                if (!empty($group))  $data["list"][] = $group;
            }
            $this->load->view('website/default/head', array("document_title" => DOCUMENT_TITLE . "Products by application"));   
            $this->load->view('website/default/header');  
            $this->load->view('website/products/products_by_application', $data);   
            $this->load->view('website/default/footer');   

        } else {

            //application info
            $data["app"]= $this->productsmodel->get_application($id);

            //products list                               
            $products = $this->productsmodel->get_products_by_application($id);
            $data["list"]["items"] = $this->productsmodel->group_into_categories($products);
            $data["list"]["controller"] = "products/view/";

            if (!empty($group))  $data["list"][] = $group;

            $this->load->view('website/default/head', array("document_title" => DOCUMENT_TITLE . "Products by application - " .$data["app"]["Name_Applications"])); 
            $this->load->view('website/default/header');  
            $this->load->view('website/products/prod_application', $data);   
            $this->load->view('website/default/footer');  

        }

    }

    function view($id = FALSE)
    {

        if ($id===FALSE) show_404();

        if (!session_check()) {
            set_referrer();
            redirect('insider', 'refresh');
        }
        
        $this->load->model('glossarymodel');

        //load product data
        $data["item"] = $this->productsmodel->get_product($id);        
        if (empty($data["item"])) show_404();

        //register products view
        $this->productsmodel->register_product_view($id, $this->session->userdata('user_id'));
        //related products list                       
        $data["rel_products"]["items"] = $this->productsmodel->get_related_products($id, $this->session->userdata('user_id'));
        $data["rel_products"]["controller"] = "products/view/";

        $data["item"]["images"] = $this->productsmodel->get_product_images($id);
        $data["item"]["applications"] = $this->productsmodel->get_product_applications($id); 
        $data["item"]["specifications"] = $this->productsmodel->get_product_specifications($id);
        $data["item"]["specifications"][] = array( "Name_Specs" => "Finishes",
                                                   "Value" => $this->productsmodel->finishes_result_into_string($this->productsmodel->get_product_finishes($id)) );

        if($data["item"]["Parent_Categories"] != "") $item["parent"] = $this->productsmodel->get_category($data["item"]["Parent_Categories"]);

        $this->load->model('wishlistsmodel'); 
        $data["wishlists"] = $this->wishlistsmodel->get_wishlists($this->session->userdata('user_id'));

        $this->load->view('website/default/head', array("document_title" => DOCUMENT_TITLE . $data["item"]["Name_Products"]));  
        $this->load->view('website/default/header');  
        $this->load->view('website/products/product_detail', $data);   
        $this->load->view('website/default/footer');   

    }

    function search_results($id = FALSE)
    {

        if ($id == FALSE) {                                     
            $search_string = $this->get_search_string();               
        } else {
            $search_string = $this->productsmodel->get_search_history_by_id($id); 
        }
        
        if ($search_string === FALSE) redirect('/products/');
        $data["search_string"] = $search_string;

        //products list                                                           
        $data["prodlist"]["items"] = $this->productsmodel->get_prod_search_results($search_string);
        $data["prodlist"]["controller"] = "products/view/";

        //categories list                                                           
        $data["catlist"]["items"] = $this->productsmodel->get_cat_search_results($search_string);
        $data["catlist"]["controller"] = "products/categories/";                

        if (((!empty($data["prodlist"]["items"])) || (!empty($data["catlist"]["items"]))) && ($id==FALSE))
            $this->productsmodel->add_to_history($search_string);


        $this->load->view('website/default/head', array("document_title" => DOCUMENT_TITLE . "Products - Search Results"));  
        $this->load->view('website/default/header');  
        $this->load->view('website/products/products_results', $data);   
        $this->load->view('website/default/footer');   

    }

    private function get_search_string() {   

        if (isset($_POST['search'])) {
            
            $search_string = strtolower(trim($this->input->post('search', TRUE))); 

        } else {

            $search_string = $this->productsmodel->get_search_string();                  
        }
                                                                      
        return $search_string;

    }

    public function pdf($id = FALSE)
    {

        if ($id===FALSE) show_404();

        if (!session_check()) {
            set_referrer();
            redirect('insider', 'refresh');
        }

        $product = $this->productsmodel->get_product($id);        
        if (empty($product)) show_404();
		if (empty($product["pdf_Products"])) return FALSE;
		
		$this->load->helper('download');
		$data = file_get_contents("./".PDF_FOLDER.$product["pdf_Products"]);
		
		force_download($product["pdf_Products"], $data);
    } 

}
                  
/* End of file welcome.php */
/* Location: ./system/application/controllers/welcome.php */
?>